/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Porsentaje;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author nan
 */
public class medPorsentaje implements ActionListener, KeyListener {

    private Conexion.ConexionBD conectar = new Conexion.ConexionBD();
    String[] titulos = {"Tipo Pago", "Valor"};
    Object[][] datos = {};
    DefaultTableModel dtmArticulos;
    private Porsentaje.PorsentajeGUI ventana = new Porsentaje.PorsentajeGUI();

    public medPorsentaje(){
        ventana.setLocationRelativeTo(null);
        ventana.setActionListeners(this);
        ventana.setKeyListener(this);
        ventana.setVisible(true);
        ventana.getjTable1().getColumn("Tipo Pago").setWidth(1);
        Actualizar();
    }

    public void actionPerformed(ActionEvent e) {

        if(e.getSource()==ventana.getjButton1()){
            Guardar();
        }
        if (e.getSource()==ventana.getjButton2()){
            Actualizar();
            ventana.dispose();
        }
    }

    public void keyTyped(KeyEvent e) {
        if (e.getSource()== ventana.getjTextField1()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) && (caracter != '.')){
                e.consume();
            }
            if (caracter == '.' && ventana.getjTextField1().getText().contains(".")){
                e.consume();
            }
        }
    }

    public void keyPressed(KeyEvent e) {
        
    }

    public void keyReleased(KeyEvent e) {

    }

    public void Guardar(){
        int select = ventana.getjTable1().getSelectedRow();
        if((select!=-1) ){
            if (ventana.getjTextField1().getText().matches("")){
                JOptionPane.showMessageDialog(null, "Ingrese Valor de porsentaje", "No ingreso Ningun Valor", JOptionPane.ERROR_MESSAGE);
            }else{
                conectar.conectar();
                Statement st;
                String a = ventana.getjTextField1().getText();
                double porsentaje = Double.parseDouble(a);
                try {
                    st = conectar.getConn().createStatement();
                    String consulta1 = "UPDATE \"TipoPago\" SET \"Valor\"=" +ventana.getjTextField1().getText()+ " WHERE \"Tipo\" = '"+ventana.getjTable1().getValueAt(select,0).toString()+"';";
                    st.executeQuery(consulta1);
                    st.close();
                    conectar.cerrar();
                } catch (SQLException ex) {

                }
            }
        }else{
            JOptionPane.showMessageDialog(null, "Debe seleccionar un elemento", "Error por seleccion nula", JOptionPane.ERROR_MESSAGE);
        }
        Actualizar();
        ventana.dispose();
    }


    private void Actualizar(){
        dtmArticulos = new DefaultTableModel(datos, titulos);
        ventana.getjTextField1().setText("");
        conectar.conectar();
        Statement st;
        double porsentaje;
        try {
            st = conectar.getConn().createStatement();
            String consulta1 = "SELECT * FROM \"TipoPago\" ;";
            ResultSet rs = st.executeQuery(consulta1);
            while (rs.next()){
                Object[] fila = {rs.getString(1),
                rs.getString(2)};
                dtmArticulos.addRow(fila);
            }
            ventana.getjTable1().setModel(dtmArticulos);
        } catch (SQLException ex) {
                Logger.getLogger(PorsentajeGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public void setVisible(boolean b) {
        ventana.setVisible(b);
        
    }
}
